package org.caixy.project.admin.controller;

import lombok.RequiredArgsConstructor;
import org.caixy.project.admin.common.convention.result.Result;
import org.caixy.project.admin.common.convention.result.Results;
import org.caixy.project.admin.common.enums.UserCodeError;
import org.caixy.project.admin.dto.req.UserLoginReqDTO;
import org.caixy.project.admin.dto.req.UserUpdateReqDTO;
import org.caixy.project.admin.dto.resp.UserLoginRespDTO;
import org.caixy.project.admin.dto.resp.UserRespDTO;
import org.caixy.project.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/api/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username")String username){
        UserRespDTO result = userService.getUserByUserName(username);
        if(result == null){
            return new Result<UserRespDTO>().setCode(UserCodeError.USER_NULL.code());
        }else {
            return Results.success(result);
        }
    }

    /**
     * 查询是否存在
     */
    @GetMapping("/api/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam String username){
        Boolean has = userService.hasUserName(username);
        return Results.success(has);
    }
    /**
     * 注册
     */
    @PostMapping("/api/v1/user")
    public Result<Void> Register(@RequestBody UserUpdateReqDTO requestParam){
        userService.Register(requestParam);
        return Results.success();
    }
    /**
     * 更新用户信息
     */
    @PutMapping("/api/v1/user")
    public Result<Void> Update(@RequestBody UserUpdateReqDTO requestParam){
        userService.Update(requestParam);
        return Results.success();
    }
    /**
     * 登录
     */
    @PostMapping("/api/v1/user/login")
    public Result<UserLoginRespDTO> Login(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDTO userLoginRespDTO = userService.Login(requestParam);
        return Results.success(userLoginRespDTO);
    }
    /**
     * 注销
     */
    @DeleteMapping("/api/v1/user/logout")
    public Result<Void> Logout(@RequestParam("username") String username){
        userService.Logout(username);
        return Results.success();
    }

}
